<?php
/*
 * 获取资讯列表
 */
class ArticlesController extends CController{
public function actionList(){
		$key = '0d291452bc581a021494f7b4bdbb3d5d';
		$apptk = isset($_GET['apptk']) ? $_GET['apptk']:'';
		if ($apptk !='' && $key == $apptk){
			$infomation = new News();//咨询列表
			$infomation = $infomation->findAll();
			if (!empty($infomation)){
				$array = array();
				$array['isok'] = true;
				foreach ($infomation as $value){
					$arr = array();
					$arr['name'] = CHtml::value($value,'title');
					$arr['description'] = CHtml::value($value, 'description');
					$arr['cover'] = CHtml::value($value, 'cover');
					$arr['created_at'] = CHtml::value($value, 'create_time');
					$array['data'][] = $arr;
				}
				echo json_encode($array);
			}else {
				$err = array(
					'isok'=>false,
					'msg' =>'数据表数据为空！',
				);
				echo json_encode($err);
			}
		}else {
			$error = array(
				'isok'=>false,
				'msg' =>'秘钥有误',
			);
			$error = json_encode($error);
			echo $error;
		}
	}
	/*
	 *数据接口方法
	 *@chenqiwen
	 *$Tablename数据库表的Model名称
	 *$Key_value_pairs键值对数组
	 */
	public function Data_interface_method($Tablename='',$Key_value_pairs=array()){
		$key = '0d291452bc581a021494f7b4bdbb3d5d';
		$apptk = isset($_GET['apptk']) ? $_GET['apptk']:'';
		$limit = isset($_GET['limit']) ? $_GET['limit']:10;//默认返回10数据
		$order = isset($_GET['order']) ? $_GET['order']:'';
		$offset = isset($_GET['lastID']) ? $_GET['lastID']:0;//默认起始位置为0
		if ($apptk !='' && $key == $apptk){
			if (empty($Key_value_pairs))
			die('字段匹配有误');
			$criteria = new CDbCriteria();
			if ($limit != '')
			$criteria->limit = $limit;
			if ($order != '')
			$criteria->order ='id '.$order;
			$criteria->offset=$offset;
			$model = new $Tablename();
			$model = $model->findAll($criteria);
			if (!empty($model)){
				$array = array();
				$array['isok'] = true;
				foreach ($model as $value){
					$arr = array();
//					reset($Key_value_pairs);
//					while (list($key, $val) = each($Key_value_pairs))
//					{
//						$arr[$key] = CHtml::value($value, $val);
//					}
					foreach ($Key_value_pairs as $key=>$val) {
						$arr[$key] = CHtml::value($value, $val);
					}
					$array['data'][] = $arr;
				}
				echo json_encode($array);
				
			}else {
				$err = array(
					'isok'=>false,
					'msg' =>'数据表数据为空！',
				);
				echo json_encode($err);
			}
		}else {
			$error = array(
				'isok'=>false,
				'msg' =>'秘钥有误',
			);
			echo json_encode($error);
			
		}
	}
	public function actionTest(){
		$this->Data_interface_method('News',
		array(
			'name'=>'title',
			'description'=>'description',
			'cover'=>'cover',
			'created_at'=>'create_time',
		));
	}	
}
?>